package com.cssw.fyzb.job.executor.config;

import com.cssw.fyzb.job.executor.model.tps.AuditResult;
import com.cssw.fyzb.job.executor.model.tps.Page;
import com.ydn.appserver.ClientConfiguration;
import com.ydn.appserver.MinaAppClient;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Feng Chen
 */
@Component
public class TpsClient {

    @Value("${tps.addrs}")
    private String addrs;

    @Value("${tps.maxConn:64}")
    private int maxConn;

    @Value("${tps.coTimeout:3000}")
    private int coTimeout;

    @Value("${tps.soTimeout:3000}")
    private int soTimeout;

    private MinaAppClient delegate;

    @PostConstruct
    public void start() {
        ClientConfiguration configuration = new ClientConfiguration();
        configuration.setServerAddressesString(addrs);
        configuration.setMaxConnectionsPerServer(maxConn);
        configuration.setSocketConnTimeoutSec(coTimeout);
        configuration.setSocketDataTimeoutSec(soTimeout);
        delegate = new MinaAppClient(configuration);
    }

    @PreDestroy
    public void close() {
        if (delegate != null) {
            delegate.close();
        }
    }

    /**
     * 获取视频审核结果
     *
     * @param vodId
     * @return 1-审核中  2-审核成功  3-审核失败
     */
    public AuditResult getAuditResult(String vodId) {
        Map<String, Object> param = new HashMap<>();
        param.put("videoId", vodId);
        return get("GetAuditResult", param, AuditResult.class);
    }


    private <T> T get(String action, Map<String, Object> params, Class<T> type) {
        Request request = new Request(action);
        for (Map.Entry<String, Object> param : params.entrySet()) {
            Object value = param.getValue();
            if (value != null) {
                request.setParameter(param.getKey(), String.valueOf(value));
            }
        }
        Response response = delegate.send(request);
        if (response.isSuccess()) {
            return response.getObject("data", type);
        }
        return null;
    }

    private <T> Page<T> page(String action, Map<String, Object> params, Class<T> type) {
        Request request = new Request(action);
        for (Map.Entry<String, Object> param : params.entrySet()) {
            Object value = param.getValue();
            if (value != null) {
                request.setParameter(param.getKey(), String.valueOf(value));
            }
        }
        Response response = delegate.send(request);
        if (response.isSuccess()) {
            Page<T> page = new Page<>();
            Integer total = response.getInteger("total");
            if (total == null) {
                total = 0;
            }
            Integer count = response.getInteger("count");
            if (count == null) {
                count = 0;
            }

            page.setTotal(total);
            page.setCount(count);
            page.setList(response.getList("list", type));
            return page;
        }

        return null;
    }

}
